<template>
  <div>
    <t-modal
      :title="title"
      :width="640"
      :visible="visible"
      :confirmLoading="confirmLoading"
      @ok="
        () => {
          $emit('ok');
        }
      "
      @cancel="
        () => {
          $emit('cancel');
        }
      "
    >
      <a-form :form="form">
        <a-form-item>
          <a-spin :spinning="confirmLoading">
            <a-form-item
              label="分类"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-tree-select
                style="width: 100%"
                placeholder="分类"
                allow-clear
                :replaceFields="{
                  children: 'children',
                  title: 'orgName',
                  value: 'id',
                }"
                :tree-data="treeData"
                v-decorator="[
                  'orgId',
                  {
                    initialValue: orgId,
                    rules: [{ required: true, message: '请选择分类！' }],
                  },
                ]"
              />
            </a-form-item>
            <a-form-item
              label="名称"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input
                placeholder="名称"
                v-decorator="[
                  'roleName',
                  { rules: [{ required: true, message: '请输入名称！' }] },
                ]"
              />
            </a-form-item>
            <a-form-item
              label="编码"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input
                placeholder="编码"
                v-decorator="[
                  'roleCode',
                  {
                    rules: [
                      {
                        required: true,
                        message: '请输入编码！',
                      },
                      {
                        pattern: /^[a-zA-Z0-9_-]+$/,
                        message: '只能输入字母和数字',
                      },
                    ],
                  },
                ]"
              />
            </a-form-item>
            <a-form-item
              label="状态"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <t-dict-select-tag
                type="select"
                dictCode="MenuStatus"
                placeholder="状态"
                v-decorator="[
                  'roleStatus',
                  {
                    initialValue: 'MenuStatus.01',
                    rules: [{ required: true, message: '请选择状态！' }],
                  },
                ]"
              />
            </a-form-item>
            <a-form-item
              label="排序"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input-number
                id="inputNumber"
                style="width: 100%;"
                placeholder="排序"
                :min="1"
                :max="9999"
                v-decorator="['roleSort']"
              />
            </a-form-item>
            <a-form-item
              label="备注"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-textarea
                :rows="4"
                placeholder="备注"
                v-decorator="['roleRemark']"
              />
            </a-form-item>
            <a-form-item class="hiddenItem">
              <a-input v-decorator="['uuid']" type="hidden" />
            </a-form-item>
          </a-spin>
        </a-form-item>
      </a-form>
    </t-modal>
  </div>
</template>

<script>
import pick from "lodash.pick";
import { TDictSelectTag } from "@/components";
// 表单字段
const fields = [
  "uuid",
  "roleId",
  "roleName",
  "roleCode",
  "roleStatus",
  "roleSort",
  "roleRemark",
];
export default {
  name: "TableModal",
  components: {
    TDictSelectTag,
  },
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    model: {
      type: Object,
      default: () => null,
    },
    type: {
      type: String,
    },
  },
  data() {
    return {
      title: "",
      confirmLoading: false,
      size: "default",
      labelCol: {
        xs: {
          span: 24,
        },
        sm: {
          span: 5,
        },
      },
      wrapperCol: {
        xs: {
          span: 24,
        },
        sm: {
          span: 17,
        },
      },
      form: this.$form.createForm(this),
      pid: "",
      levelId: 1,
      treeData: [],
      orgId: 0,
    };
  },
  mounted() {},
  created() {
    // 防止表单未注册
    fields.forEach((v) => this.form.getFieldDecorator(v));

    // 当 model 发生改变时，为表单设置值
    this.$watch("model", () => {
      this.model && this.form.setFieldsValue(pick(this.model, fields));
    });
  },
  methods: {
    add(val, data) {
      this.treeData = data;
      this.orgId = val.orgId;
    },
    edit(val, data) {
      this.treeData = data;
      this.orgId = val.orgId;
    },
  },
};
</script>

<style></style>
